package main

import (
	"github.com/asim/go-micro/plugins/wrapper/monitoring/prometheus/v3"
	"github.com/asim/go-micro/plugins/wrapper/ratelimiter/uber/v3"
	"github.com/asim/go-micro/plugins/wrapper/trace/opentracing/v3"
	"github.com/asim/go-micro/v3"
	"github.com/zw2582/ginlib"
	"learn.micro.service.product/common"
	"learn.micro.service.product/handlers"
	"learn.micro.service.product/proto/product"
)

func main() {
	//初始化ini配置
	ginlib.InitIni()

	//初始化日志
	ginlib.InitLogger()

	//初始化consul配置注册中心
	common.InitConsulConfig()

	//初始化数据库
	//models.DB = common.InitDbFromConsul()

	//创建jaeger链路追踪器
	t, o := common.CreateJaegerFromIni()
	defer o.Close()

	//异步启动监控服务：普罗米修斯
	common.PrometheusBoot()

	//define server and init
	ser := micro.NewService(
		micro.Name(ginlib.APP_NAME),
		//配置consul注册中心
		micro.Registry(common.CreateRegistryFromConsul()),
		//配置jaeger链路追踪
		micro.WrapHandler(opentracing.NewHandlerWrapper(t)),
		//配置限流RPS
		micro.WrapHandler(ratelimit.NewHandlerWrapper(1000)),
		//配置监控普罗米修斯，监控服务状态
		micro.WrapHandler(prometheus.NewHandlerWrapper()),
		)

	//可选，直接设置服务的地址和端口
	ser.Init(micro.Address("127.0.0.1:8080"))

	//register handler
	if err := product.RegisterProductHandler(ser.Server(), &handlers.ProductHandler{}); err != nil {
		panic(err)
	}

	//run micro server
	if err := ser.Run(); err != nil {
		panic(err)
	}
}
